<?php
declare(strict_types=1);

namespace App\ZxAdmin\Content;

use App\Controller;
use Field\SysCateAccessConst;
use Field\SysCateAccessRow;
use Field\SysCateConst;
use Framework\Libs\Model;

// hook controller_zxadmin_content_cate_access_use.php

/**
 * @title   分类管理
 * @auth    true
 * @login   true
 * @menu    false
 * @component vlist/index.vue
 * @icon el-icon-menu
 * @rank    1
 */
class CateAccess extends Controller
{
    // hook controller_zxadmin_content_cate_access_public_start.php

    // hook controller_zxadmin_content_cate_access_public_end.php
    public const Conf = [
        'pageSize' => 25,
        'pageSizes' => [10, 15, 20, 25, 30, 50, 100, 200, 500],
        'remoteSort' => 0,
        'remoteFilter' => 0,
        'remoteSummary' => 0,
        'hidePagination' => 1,
        'hideRefresh' => 0,
        'hideSetting' => 1,
        'hideDo' => 1,
        'add' => 0,
        'start' => 0,
        'stop' => 0,
        'delete' => 0,
        'import' => 0,
        'export' => 0,

    ];
    /**
     * @title   列操作
     */
    public const Tools = [];
    /**
     * @title   列配置
     */
    public const Columns = [];
    /**
     * @title   筛选
     */
    public const Filter = [];
    /**
     * @title   添加内容
     */
    public const AddForm = [];
    /**
     * @title   编辑内容
     */
    public const EditForm = [];

    /**
     * @title  数据库连接
     * @rank    99
     */
    public function action_Conf_PUT()
    {
        // hook controller_zxadmin_content_cate_access_index_start.php
        $Columns = [
            [
                'label' => "分类ID",
                'prop' => "CateID",
                'width' => "100"
            ],
            [
                'label' => "权限组",
                'prop' => "RoleName",
                'width' => "200"
            ],
            [
                'type' => "dict",
                'label' => "阅读",
                'prop' => "AllowRead",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "dict",
                'label' => "新帖",
                'prop' => "AllowThread",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "dict",
                'label' => "回复",
                'prop' => "AllowPost",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "dict",
                'label' => "上传",
                'prop' => "AllowAttach",
                'editable'=>1,
                'width' => "60"
            ],
              [
                'type' => "dict",
                'label' => "下载",
                'prop' => "AllowDown",
                'editable'=>1,
                'width' => "60"
            ],
            ];

        $Tools =[
        ['Name'=>'阅读','Field'=>'AllowRead','Do'=>'Dict','Dict'=>'Status'],
        ['Name'=>'新帖','Field'=>'AllowThread','Do'=>'Dict','Dict'=>'Status'],
        ['Name'=>'回复','Field'=>'AllowPost','Do'=>'Dict','Dict'=>'Status'],
        ['Name'=>'上传','Field'=>'AllowAttach','Do'=>'Dict','Dict'=>'Status'],
        ['Name'=>'下载','Field'=>'AllowDown','Do'=>'Dict','Dict'=>'Status'],
        ];
        // hook controller_zxadmin_content_cate_access_index_end.php

        $this->response('0000', ['Tabs' => [], 'Conf' => self::Conf, 'Filter' => self::Filter, 'Tools' =>$Tools, 'Columns' => $Columns]);

    }

    /**
     * @title  数据表
     * @rank    99
     */
    public function action_Index()
    {
        // hook controller_zxadmin_content_cate_access_index_start.php
        $CateID = $this->request->param('CateID', 0);
        $this->CheckEmpty([$CateID],['分类ID']);
        $role =  $this->SysRole->data_kv;
        $cate = $this->SysCateAccess->select([SysCateAccessConst::CateID => $CateID],[],0,0,'*','RoleID');
        $add=[];
        foreach ($role as $RoleID => $RoleName){
            if(isset($cate[$RoleID])){
                $cate[$RoleID]['RoleName'] = $RoleName;
            }else{
                $add[]=['CateID' => $CateID, 'RoleID' => $RoleID];
                $cate[$RoleID] =(new SysCateAccessRow(['CateID' => $CateID, 'RoleID' => $RoleID, 'RoleName' => $RoleName]))->toArray();
            }
        }
        !empty($add) && $this->SysCateAccess->insertALL($add);

        // hook controller_zxadmin_content_cate_access_index_end.php
        $this->response('0000', ['data'=>array_values( $cate)]);
    }

    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zxadmin_content_cate_access_field_post_start.php
        $cond = [];
        $cond['CateID'] = $this->request->param('CateID', 0);
        $cond['RoleID'] = $this->request->param('RoleID', 0);
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field], ['待修改字段']);
        $field_fields = ['AllowRead', 'AllowThread', 'AllowPost', 'AllowAttach', 'AllowDown'];
        !in_array($field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($field, SysCateAccessConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($field) {
            case 'AllowRead':
            case 'AllowThread':
            case 'AllowPost':
            case 'AllowAttach':
            case 'AllowDown':
                $update[$field] = intval($value);
                break;
        }

        $r = !empty($update) ? $this->SysCateAccess->update($cond, $update) : false;
        // hook controller_zxadmin_content_cate_access_field_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    /**
     * @title   批量更新字段
     * @rank    40
     */
    public function action_Update_POST()
    {
        // hook controller_zxadmin_content_cate_disable_start.php
        $data = $this->request->param('data', []);
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$data,$field], ['操作数据为空','字段']);
        $field_fields = ['AllowRead', 'AllowThread', 'AllowPost', 'AllowAttach', 'AllowDown'];
        !in_array($field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($field, SysCateAccessConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($field) {
            case 'AllowRead':
            case 'AllowThread':
            case 'AllowPost':
            case 'AllowAttach':
            case 'AllowDown':
                $update[$field] = intval($value);
                break;
        }

        $r = !empty($update) ? $this->SysCateAccess->updateByIndex($data, $update) : false;
        // hook controller_zxadmin_content_cate_disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    // hook controller_sys_api_stat_end.php
}
?>